package com.example.webdemo.service.impl;

import com.example.webdemo.dao.ArticleMapper;
import com.example.webdemo.dao.ArticlePopularityMapper;
import com.example.webdemo.model.*;
import com.example.webdemo.service.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zp
 * @version 1.0
 * @description: ArticleServiceImpl
 * @date 2023/7/31 19:13
 */
@Service
public class ArticlePopularityServiceImpl implements ArticlePopularityService {
    @Resource
    private ArticlePopularityMapper articlePopularityMapper;
    @Resource
    private ArticleInfoService articleInfoService;
    @Resource
    private UserInfoService userInfoService;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return articlePopularityMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(ArticlePopularity record) {
        return articlePopularityMapper.insert(record);
    }

    @Override
    public int insertSelective(ArticlePopularity record) {
        return articlePopularityMapper.insertSelective(record);
    }

    @Override
    public ArticlePopularity selectByPrimaryKey(Integer id) {
        return articlePopularityMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(ArticlePopularity record) {
        return articlePopularityMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(ArticlePopularity record) {
        return articlePopularityMapper.updateByPrimaryKey(record);
    }

    @Override
    public List<ArticlePopularity> selectAll() {
        return articlePopularityMapper.selectAll();
    }

    @Override
    public ArticlePopularity selectByEndorseIdAndType(Integer articleInfoId,Integer endorseId, Integer endorseType) {
        return articlePopularityMapper.selectByEndorseIdAndType(articleInfoId,endorseId, endorseType);
    }

    @Override
    public Integer selectByUserIdAndType(Integer userId, Integer endorseType) {
        return articlePopularityMapper.selectByUserIdAndType(userId, endorseType);
    }

    @Override
    public List<Integer> selectByUserIdAndTypeIds(Integer userId, Integer endorseType) {
        return articlePopularityMapper.selectByUserIdAndTypeIds(userId, endorseType);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean addArticlePopularity(Integer articleInfoId, Integer type, Integer endorseId,Integer userId) {
        ArticlePopularity articlePopularity = new ArticlePopularity();
        articlePopularity.setArticleInfoId(articleInfoId);
        articlePopularity.setUserId(userId);
        articlePopularity.setEndorseType(type);
        articlePopularity.setEndorseId(endorseId);

        UserInfo userInfo = userInfoService.selectByPrimaryKey(userId);

        int count = articlePopularityMapper.insertSelective(articlePopularity);
        ArticleInfo articleInfo = articleInfoService.selectByPrimaryKey(articleInfoId);
        if(type == 0){
            articleInfo.setLikeNum(articleInfo.getLikeNum() + 1);
            userInfo.setArtCount(userInfo.getArtCount() + 1);
        }else if(type == 1){
            articleInfo.setShareNum(articleInfo.getShareNum() + 1);
            userInfo.setFollowCount(userInfo.getFollowCount() + 1);
        }else{
            articleInfo.setCollectNum(articleInfo.getCollectNum() + 1);
            userInfo.setFansCount(userInfo.getFansCount() + 1);
        }
        count += articleInfoService.updateByPrimaryKeySelective(articleInfo);
        count += userInfoService.updateByPrimaryKeySelective(userInfo);
        return count > 0;
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteByArticleIdAndType(Integer articleInfoId, Integer endorseId, Integer type) {
        int count = articlePopularityMapper.deleteByArticleIdAndType(articleInfoId,endorseId,type);
        ArticleInfo articleInfo = articleInfoService.selectByPrimaryKey(articleInfoId);
        Integer userId = articleInfo.getUserId();
        UserInfo userInfo = userInfoService.selectByPrimaryKey(userId);

        if(type == 0 && articleInfo.getLikeNum()>0){
            articleInfo.setLikeNum(articleInfo.getLikeNum() - 1);
        }else if(type == 1 && articleInfo.getShareNum()>0){
            articleInfo.setShareNum(articleInfo.getShareNum() - 1);
        }else if(type == 2 && articleInfo.getCollectNum()>0){
            articleInfo.setCollectNum(articleInfo.getCollectNum() - 1);
        }

        if(type == 0 && userInfo.getArtCount()>0){
            userInfo.setArtCount(userInfo.getArtCount() - 1);
        }else if(type == 1 && userInfo.getFollowCount()>0){
            userInfo.setFollowCount(userInfo.getFollowCount() - 1);
        }else if(type == 2 && userInfo.getFansCount()>0){
            userInfo.setFansCount(userInfo.getFansCount() - 1);
        }
        count += articleInfoService.updateByPrimaryKeySelective(articleInfo);
        count += userInfoService.updateByPrimaryKeySelective(userInfo);
        return count>0;
    }


}
